<?php

class InfoQnaAddAction extends BaseAction 
{
	/**
	 * @isnull false
	 * @label Question
	 */
	public $question;
	public $answer;
	public $reference;
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;

	public $programme_id;
	public $onair_episode_no;

	public $programme_title_cn;
	public $programme_title_en;
	public $programme_path;
 
    /**
	 * @filter datetime
	 * @label Publish Date/Time 
	 */
    public $publish_datetime;
    
    /**
	 * @filter datetime
	 * @label Expiry Date/Time
	 */
	public $expire_datetime;

	public $ut;
	public $reset;
	public function execute(){

		if (!is_null($this->reset))
		{
			$this->setData('type', 'qa');
			$this->setData('act', 'add');
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check programme and on air episode no
			$check_exists = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
			if ($check_exists == '')
			{
				$this->addError('Programme or Episode', array('0103', 'Programme or Episode'));
				$this->init_page_data();
				return 'logic-error';
			}
            
            if (!is_null($this->expire_datetime))
			{
				if ($this->publish_datetime > $this->expire_datetime)
				{
					$this->init_page_data();
					$this->addError('Publish Date/Time && Expiry Date/Time', array('0033', 'Publish Date/Time && Expiry Date/Time'));
					return 'filter-error';
				}
			}

			$insert_id = $this->add_info_q_a();
			if (FALSE != $insert_id)
			{
				$this->setData('type', 'qa');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return "succ";
		}
		catch(Exception $e)
		{
			error_log("Add QA Fails: " . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * add cast base information
	 */
	private function add_info_q_a()
	{
		$info_q_a = new info_q_a();
		$info_q_a->set($this);
		$info_q_a->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_q_a->created_by = CommonUtils::get_user();
        $info_q_a->modified_at = $info_q_a->created_at;
        $info_q_a->modified_by = $info_q_a->created_by;
		$info_q_a->status = AC::STATUS_NEW;
		$info_q_a->episode_id = episode::getEpisodeID($this->programme_id, $this->onair_episode_no);
		$info_q_a->id = $insert_id = Model::$dbo['w']->save($info_q_a);

		if ($insert_id > 0)
		{
			$info_q_a->add_change_log();

            $primary_key = array(
                'id'    => $insert_id
            );
            $fields = array(
                'question',
                'answer',
                'reference'
            );
            CommonUtils::filterC2A0('info_q_a', $primary_key, $fields);

			$this->addInfo("info_q_a:".AC::LOG_OP_CREATE.":".$insert_id, "add info_q_a success", json_encode($info_q_a));
			return $insert_id;
		}
		else
		{
			$this->addError("info_q_a:".AC::LOG_OP_CREATE.":".$insert_id, "add info_q_a error", json_encode($info_q_a));
			return false;
		}

	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$q_a = new info_q_a();
		$ref = new ReflectionObject($this);
		$props = $ref->getProperties();
		foreach ($props as $prop)
		{
			if ($prop->isPublic())
			{
				$prop_name = $prop->getName();
				$q_a->$prop_name = $this->$prop_name;
			}
		}
		$this->setData('qa', $q_a);
		$this->setData('publishLog', '');

		//jump page
		$this->setData('type', 'qa');
		$this->setData('act', 'add');
		$this->setData('step', 1);
	}
}
?>
